package com.example.larou.Impl;


import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.example.larou.entity.*;
import com.example.larou.mapper.ItemMapper;
import com.example.larou.mapper.TbProduct;
import com.example.larou.mapper.UserMapper;
import com.example.larou.utils.Adapter;
import com.example.larou.utils.OrderCodeFactory;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Misaka  Mikoto
 */
@Service
@Slf4j
public class AddItemServiceImpl extends Adapter {
    @Resource
    private ItemMapper itemMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private TbProduct tbProduct;
    @Override
    public SaResult insertItem(Item item){
        System.err.println("添加商品");
        Object loginIdByToken = StpUtil.getLoginIdByToken(StpUtil.getTokenValue());
        if (!StpUtil.isLogin()){
            return SaResult.error("请先登录");
        }
        int seller_id= Integer.parseInt(loginIdByToken.toString());
        boolean add = itemMapper.inserItem(item.getItem_name(),item.getDescription(),item.getPrice(),
                item.getQuantity(),item.getImage(), seller_id);
        System.err.println(seller_id);
        if (add){
            return  SaResult.ok("添加物品成功");
        }
       return SaResult.error("添加失败");

    }


    public SaResult addCart(Cart cart){
        Object loginIdByToken = StpUtil.getLoginIdByToken(StpUtil.getTokenValue());
        if (!StpUtil.isLogin()){
            return SaResult.error("请先登录");
        }
        cart.setUser_id(Integer.parseInt(loginIdByToken.toString()));
        boolean add= itemMapper.addCart(cart.getUser_id(), cart.getItem_id(), cart.getQuantity(), cart.getPrice());
        if (add){
            return  SaResult.ok("添加购物车成功");

        }
        return SaResult.error("添加失败");
    }
    public SaResult addEmail(Email email){
        StpUtil.checkLogin();
        Object loginIdByToken = StpUtil.getLoginIdByToken(StpUtil.getTokenValue());
        int user_id= Integer.parseInt(loginIdByToken.toString());
        boolean selectUserID= userMapper.selectUserId(user_id);
        if (selectUserID) {
            boolean addEmail =itemMapper.addEmail(user_id,email.getDevice_sender(),email.getRecipient(), email.getEmail_name(), email.getContent(),email.getDevice_id());
            if (addEmail){
                return SaResult.ok("添加成功").setData(email.toString());
            }
            return SaResult.error("id错误");
        }
        return SaResult.error("添加失败");
    }

    public SaResult createOrder(CreateOrderVo createOrderVo ,long userId){
        Order order=new Order();
        String orderNumber=OrderCodeFactory.getOrderCode( userId);

        order.setOrderNumber(orderNumber);
        order.setPaymentMethod(createOrderVo.getPaymentMethod().getValue());
        order.setProductId(createOrderVo.getProductId());
        order.setShopId(createOrderVo.getShopId());
        order.setUserId(userId);
        order.setStatus(createOrderVo.getStatus().getValue());
        order.setAddress(createOrderVo.getAddress());
        order.setStatus(createOrderVo.getStatus().getValue());
        order.setPrice(createOrderVo.getPrice());
        order.setQuantity(createOrderVo.getQuantity());
        order.setShopId(createOrderVo.getShopId());

        if(tbProduct.createOrder(order)) {
            return SaResult.ok().setData("创建成功");
        }
        return SaResult.error("创建失败");
    }
    public SaResult displayOrderInf(int userId){
        List<OrderInfRes> orderList = tbProduct.getProductByUserId(userId);
        return SaResult.ok().setData(orderList);
    }
}
